package ca.nfb.media.nfbPlayer.views
{
	import ca.nfb.media.nfbPlayer.ComModule;
	import ca.nfb.media.nfbPlayer.ui.HitZone;
	import ca.nfb.media.nfbPlayer.ui.ToolTip;
	import ca.turbulent.media.Pyro;
	import ca.turbulent.media.events.PyroEvent;
	import ca.turbulent.media.events.StatusUpdateEvent;
	
	import flash.display.*;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	
	public class BookmarksView extends NFBPlayerView
	{
		
		private var comm				:ComModule;
		private var dbg					:Sprite;
		public var bookmarks			:Array = new Array();
		public var bookmarksDrawn		:Boolean = false;
		public var bookmarkHolder_mc	:MovieClip = new MovieClip();
		public var bookmarksList		:Array = new Array();
		private var bookmarksHideTimer	:Timer = new Timer(3000, 1);
		private var fileURLCache		:String = "";
		private var localDuration		:Number = 0;
		private var isOverScreen		:Boolean = false;
		 
		public function BookmarksView()
		{
			super();
			comm = ComModule.getInstance();
			comm.addEventListener(ComModule.BOOKMARKS_RECEIVED_EVENT, bookmarksReceived, false, 0, true);
			bookmarksHideTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone, false, 0, true);
			this.visible = false;
			
			
		}
		
		override protected function streamStatusUpdated(evt:StatusUpdateEvent):void 
		{ 
			
		}
		
		override protected function streamStopped(evt:PyroEvent):void
		{

		}
		
		override protected function streamStarted(evt:PyroEvent):void 
		{ 
			core.hitZone.enable();
			core.hitZone.addEventListener(HitZone.MOUSED_OVER_EVENT, hitZoneMousedOver, false, 0, true);
			core.hitZone.addEventListener(HitZone.MOUSED_OUT_EVENT, hitZoneMousedOut, false, 0, true);
			
		}	
		
		
		private function timerDone(evt:TimerEvent):void
		{
			hideAllBookmarks();
		}
		
		private function hitZoneMousedOver(evt:Event):void
		{
			if (bookmarksHideTimer.running)
				bookmarksHideTimer.stop();
				
			this.showAllBookmarks();
		}
		
		private function hitZoneMousedOut(evt:Event):void
		{
			bookmarksHideTimer.delay = 500;
			
			if (bookmarksHideTimer.running)
				bookmarksHideTimer.stop();
				
			bookmarksHideTimer.start();	
		}
		
		public function clearBookmarks():void
		{
			if(bookmarkHolder_mc.numChildren < 1)
			{
				bookmarksList = new Array();
				if (this.contains(bookmarkHolder_mc))
					this.removeChild(bookmarkHolder_mc);
				
				bookmarkHolder_mc = new MovieClip();
				addChild(bookmarkHolder_mc);
				bookmarksList = new Array();
				bookmarksDrawn = false;
				return;
			}	

			for(var i:int=0; i<bookmarksList.length; ++i)
			{
				if(bookmarkHolder_mc.contains(bookmarksList[i]))
					bookmarkHolder_mc.removeChild(bookmarksList[i]);
			}
			
			if (this.contains(bookmarkHolder_mc))
				this.removeChild(bookmarkHolder_mc);
			
			bookmarkHolder_mc = new MovieClip();
			addChild(bookmarkHolder_mc);
			bookmarksList = new Array();
			bookmarksDrawn = false;
		}
		
		public function reset(e:Event=null):void
		{
			clearBookmarks();
			if(player.duration > 0 && comm.bookmarksReceived)
			{
				if (fileURLCache != player.urlDetails.streamName)
				{
					fileURLCache = player.urlDetails.streamName;
					localDuration = player.duration;
					createBookmarks();
				}
			}	
				
		}
		
		private function bookmarksReceived(e:Event):void
		{
			reset();
		}
		
		private function hideInfos(e:*=null):void
		{
			core.toolTip.hideToolTip();
		}
		
		private function showInfos(e:MouseEvent):void
		{
			var bkm:MovieClip = MovieClip(e.target);
			var point:Point = new Point(bkm.x-1, bkm.y-20);
			point = this.localToGlobal(point);
			var idx:int = int(e.currentTarget.name.replace("book_", ""))
			core.toolTip.showFixedToolTip(String(bookmarks[idx].time), bookmarks[idx][ComModule.getLang().toLowerCase()], point, this, 0);
			if (bookmarksHideTimer.running)
				bookmarksHideTimer.stop();
		}
		
		private function doSeek(e:MouseEvent):void
		{
			var idx:int = int(e.currentTarget.name.replace("book_", ""))
			var time = convertToSeconds(bookmarks[idx].time) ;
			var pointIsBuffered:Boolean = time > player.timeOffset && player.timeToPixel(time) < core.controlBar.movableBars_mc.bufferingBar_mc.width;
				
			if (player.streamType == Pyro.STREAM_TYPE_TRUE_STREAM)
			{	
				player.seek(time);
			}
			else if (player.streamType == Pyro.STREAM_TYPE_PROXIED_PROGRESSIVE)
			{
				if (pointIsBuffered)
				{	
					player.seek(time);
				}
				else
				{	
					var tURL:String = ComModule.getInstance().latestFileURL;
					var fileURL:String="";
					if (tURL.indexOf("?start=") > 0)
					{	
						fileURL = tURL.substring(0, tURL.indexOf("?"));
					}
					else
					{
						fileURL = tURL;
					}
					fileURL += "?start="+String(time);
					player.play(fileURL);
				}	
			}
			else if (player.streamType == Pyro.STREAM_TYPE_PROGRESSIVE)
			{
				if (pointIsBuffered)	
					player.seek(time);
			}
		}
		
		private function createBookmarks():void
		{
			trace("createBookmark");
			bookmarksList = new Array();
			bookmarks = comm.latestBookmarks;
			
			for(var i:int=0; i<bookmarks.length; i++)
			{
				var time = convertToSeconds(bookmarks[i].time);
				var b:BookmarkPos 	= new BookmarkPos();
				b.name 				= "book_"+i;	
				trace("create bookmark "+i);	
				b.timeStamp 		= time;
				b.addEventListener(MouseEvent.ROLL_OVER, showInfos, false, 0, true);
				b.addEventListener(MouseEvent.ROLL_OUT, hideInfos, false, 0, true);
				b.addEventListener(MouseEvent.CLICK, doSeek, false, 0, true);
				b.addEventListener(FocusEvent.FOCUS_IN, showInfosFromTab, false, 0, true);
				b.addEventListener(FocusEvent.FOCUS_OUT, hideInfos, false, 0, true);
				b.buttonMode = true;
				core.screenReaderManager.addAccess(b, bookmarks[i].time+" "+bookmarks[i]['en'], bookmarks[i].time+" "+bookmarks[i]['fr']);	
				bookmarkHolder_mc.addChild(b);
				bookmarksList.push(b);
				
			}
			positionBookmarks();
			bookmarksDrawn = true;
			this.visible = true;
			bookmarksHideTimer.start();
		}
		
		private function positionBookmarks():void
		{
			for (var i:Number=0; i<bookmarkHolder_mc.numChildren; ++i)
			{
				var b:* = bookmarkHolder_mc.getChildAt(i);
				b.x = player.timeToPixel(b.timeStamp);
			}
		}
		
		public function showAllBookmarks():void
		{
			bookmarkHolder_mc.visible = true;
			for (var i:Number=0; i<bookmarksList.length; ++i)
			{
				InteractiveObject(bookmarksList[i]).tabIndex = i+1;
				InteractiveObject(bookmarksList[i]).tabEnabled = true;
			}
		}
		
		public function showInfosFromTab(e:FocusEvent):void
		{
			var bkm:MovieClip = MovieClip(e.target);
			var point:Point = new Point(bkm.x-1, bkm.y-20);
			point = this.localToGlobal(point);
			var idx:int = int(e.currentTarget.name.replace("book_", ""))
			core.toolTip.showFixedToolTip(String(bookmarks[idx].time), bookmarks[idx][ComModule.getLang().toLowerCase()], point, this, 0);
			if (bookmarksHideTimer.running)
				bookmarksHideTimer.stop();
		}
		
		public function hideAllBookmarks():void
		{
			bookmarkHolder_mc.visible = false;
		}
		
		
		override protected function sizeChanged(evt:PyroEvent=null):void
		{
			bookmarkHolder_mc.visible = false;
			positionBookmarks();
			bookmarkHolder_mc.visible = true;
		}
		
		private function convertToSeconds(mnss:String):int
		{
			var tmp		:Array = mnss.split(":");
			var total	:int;
			
			if(tmp.length == 2) // min + sec
			{
				var min	:int = tmp[0];
				var sec	:int = tmp[1];
				
				total = (min*60) + sec
			}
			
			return total;
		}
		
		override protected function addedToStage(evt:Event):void 
		{ 
			registerStageEvents();	
		}
	}
}